package cn.com.zhbs.login.controller;

import cn.com.zhbs.common.Result.SimpleFactory.ResultFactory;
import cn.com.zhbs.login.service.LoginService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author 陈治杰
 * @Title: LoginController
 * @Description:
 * @date 2019/2/1314:49
 */
@Controller
@RequestMapping("login")
public class LoginController {

    @Resource
    private LoginService loginService;

       /**
         * @Description: 随机验证码
         * @param
         * @return 验证码
         * @throws
         * @author 陈治杰
         * @date 2019/7/8 17:02
         */
    @RequestMapping(value = "loginCode", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> loginCode(HttpServletRequest request){
        HttpSession session=request.getSession();
        //在Java中怎么把1到9999的数字转成 4位字符串,左边补0 如 0001 0002 0003
            String var = new String();
            for (int j = 0; j < 4; j++) {
                Random r = new Random();
                if (r.nextInt(2) < 1) {
                    String chars = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
                    var = var + chars.charAt((int) (Math.random() * 25));
                } else {
                    String chars = "123456789";
                    var = var + chars.charAt((int) (Math.random() * 9));
                }

            }
        System.out.println(var);
        session.setAttribute("loginCode",var);
        return ResultFactory.success(var).display();
    }


       /**
         * @Description: 登录操作
         * @param
         * @return ${return_type}
         * @throws
         * @author 陈治杰
         * @date 2019/7/8 17:03
         */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> login(@RequestBody JSONObject jsonObject, HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> requestMap=new HashMap<>();
        HttpSession session=request.getSession();
        try{
            /*if(session.getAttribute("loginCode").equals(jsonObject.get("code").toString().toUpperCase())){*/
                return loginService.login_account(jsonObject, request);
            /*}else{
                return ResultFactory.failure("0","验证码错误",null).display();
            }*/
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultFactory.failure("0","服务器错误",null).display();
    }

       /**
         * @Description: 登出操作
         * @param
         * @return ${return_type}
         * @throws
         * @author 陈治杰
         * @date 2019/7/8 17:03
         */
    @RequestMapping(value = "logout", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> logout(HttpServletRequest request, HttpServletResponse response){
        try{
            HttpSession session=request.getSession();
            session.invalidate();
            return ResultFactory.success().display();
        }catch (Exception e){
            e.printStackTrace();
            return ResultFactory.failure().display();
        }
    }
}
